package com.wj.console.common.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.base.util.SessionUtils;
import com.wj.console.constant.Constant;

public class GlobalInterceptor extends HandlerInterceptorAdapter {

	private final static Logger log = Logger.getLogger(GlobalInterceptor.class);

	@Autowired
	private Constant constant;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String path = request.getRequestURI();
		if(path.contains("/toLogin")|| path.contains("/register/")|| path.contains("/omFlowInterface/")){
			return true;
		}
		try {
			//user为session中的用户
			Object user = SessionUtils.getUser(request);
			if(user == null){
				response.sendRedirect(constant.getDomainName());
				return false;
			}
		} catch (Exception e) {
			response.sendRedirect(constant.getDomainName());
			return false;
		}
		return super.preHandle(request, response, handler);
	}

}
